package districting;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class AECResultsByPB2010 {

   public static final String CSV_PATTERN = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";

   public static final int INFORMAL = 999;

   private Nation nation;

   public AECResultsByPB2010(Nation nation) {
      this.nation = nation;
   }
   
   public void importPrimaryVotes(State state){
      try {
         importPrimaryVotesData(
               new FileReader(
                     "data/HouseStateFirstPrefsByPollingPlaceDownload-15508-"+state.getName()+".csv"),
               state);
      } catch (FileNotFoundException e) {
         System.out.println(e.getMessage());
      } catch (IOException e) {
         System.out.println(e.getMessage());
      }
   }

   public void importPrimaryVotes() {
      for(State state : nation.getStates()){
         importPrimaryVotes(state);
      }
   }

   private void importPrimaryVotesData(FileReader results, State state) {
      Scanner sc = new Scanner(results);

      // Skip header lines
      sc.nextLine();
      sc.nextLine();

      while (sc.hasNext()) {
         String str = sc.nextLine();
         String[] field;
         field = str.split(CSV_PATTERN);

         int divisionID = Integer.valueOf(field[1]);
         String divName = field[2];
         int pbID = Integer.valueOf(field[3]);
         String pbName = field[4];
         int candidateID = Integer.valueOf(field[5]);
         String surname = field[6];
         String givenname = field[7];
         int ballotPos = Integer.valueOf(field[8]);
         boolean incumbent = (field[10] == "Y");
         String partyAb = field[11];
         String partyName = field[12];
         int votes = Integer.valueOf(field[13]);
         double swing = Double.valueOf(field[14]);

         if (candidateID != INFORMAL) {

            Division division = state.getDivision(divisionID, DivisionMethod.STATUS_QUO);

            Party party;
            try {
               party = nation.getParty(partyAb);
            } catch (NullPointerException e) {
               party = new Party(partyName, partyAb, Color.black);
               nation.addParty(party);
            }

            Candidate candidate;
            try {
               candidate = division.getCandidatePrimary(candidateID);
            } catch (NullPointerException e) {
               candidate = new Candidate(candidateID, surname, givenname,
                     division, party, ballotPos, incumbent);
               division.addCandidatePrimary(candidate);
            }

            PollingBooth pb;
            try {
               pb = division.getPB(pbID);
            } catch (NullPointerException e) {
               /*
                * System.out.println("pb not found: " + pbName + " with id " +
                * pbID + " in Division " + divName + " in state " +
                * state.getName());
                */
               continue;
            }

            pb.addResultPrimary(candidate, votes);
         }

      }

   }

   public void import2PP() {
      try {
         import2PPData(new FileReader(
               "data/HouseTppByPollingPlaceDownload-15508.csv"));
      } catch (FileNotFoundException e) {
         System.out.println(e.getMessage());
      } catch (IOException e) {
         System.out.println(e.getMessage());
      }
   }
   
   public void import2PP(State state) {
      try {
         import2PPData(new FileReader(
               "data/HouseTppByPollingPlaceDownload-15508.csv"), state);
      } catch (FileNotFoundException e) {
         System.out.println(e.getMessage());
      } catch (IOException e) {
         System.out.println(e.getMessage());
      }
   }

   private void import2PPData(FileReader results) {
      Scanner sc = new Scanner(results);

      // Skip header lines
      sc.nextLine();
      sc.nextLine();

      while (sc.hasNext()) {
         String str = sc.nextLine();
         String[] field;
         field = str.split(CSV_PATTERN);

         String stateName = field[0];
         int divisionID = Integer.valueOf(field[1]);
         String divName = field[2];
         int pbID = Integer.valueOf(field[3]);
         String pbName = field[4];
         int LaborVotes = Integer.valueOf(field[5]);
         Double LaborPercentage = Double.valueOf(field[6]);
         int CoalitionVotes = Integer.valueOf(field[7]);
         Double CoalitionPercentage = Double.valueOf(field[8]);
         int TotalVotes = Integer.valueOf(field[9]);
         double swing = Double.valueOf(field[10]);

         State state = nation.getState(stateName);

         Division division = state.getDivision(divisionID, DivisionMethod.STATUS_QUO);

         try {
            PollingBooth pb = division.getPB(pbID);

            pb.addResult2PP(LaborVotes, CoalitionVotes);
         } catch (NullPointerException e) {
            System.out.println("AECResults. State = " + stateName
                  + ", Division = " + divName + ", pb = " + pbName + pbID);
         }

      }

   }

   private void import2PPData(FileReader results, State onlyThisState) {
      Scanner sc = new Scanner(results);

      // Skip header lines
      sc.nextLine();
      sc.nextLine();

      while (sc.hasNext()) {
         String str = sc.nextLine();
         String[] field;
         field = str.split(CSV_PATTERN);

         String stateName = field[0];
         int divisionID = Integer.valueOf(field[1]);
         String divName = field[2];
         int pbID = Integer.valueOf(field[3]);
         String pbName = field[4];
         int LaborVotes = Integer.valueOf(field[5]);
         Double LaborPercentage = Double.valueOf(field[6]);
         int CoalitionVotes = Integer.valueOf(field[7]);
         Double CoalitionPercentage = Double.valueOf(field[8]);
         int TotalVotes = Integer.valueOf(field[9]);
         double swing = Double.valueOf(field[10]);

         State state = nation.getState(stateName);
         if(state != onlyThisState){
            continue;
         }

         Division division = state.getDivision(divisionID, DivisionMethod.STATUS_QUO);

         try {
            PollingBooth pb = division.getPB(pbID);

            pb.addResult2PP(LaborVotes, CoalitionVotes);
         } catch (NullPointerException e) {
            System.out.println("AECResults. State = " + stateName
                  + ", Division = " + divName + ", pb = " + pbName + pbID);
         }
         

      }

   }
   
}